package visao.tipoVeiculo;

import visao.usuario.InterfaceLogin;
import java.awt.*;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import modelo.dominio.TipoVeiculo;
import modelo.persistencia.ControleAcesso;
import modelo.persistencia.PersistenciaTipoVeiculo;

/*
 * Essa classe tem é responsavel pela interface que realiza uma alteração 
 * de um tipo de veículo =
 */
public class InterfaceAlterarTipoVeiculo extends javax.swing.JFrame{
	
	private final PersistenciaTipoVeiculo persistenciaTipoVeiculo = new PersistenciaTipoVeiculo();
	
	/*
	 * Construtor da classe Interface Alterar Tipo de um veículo 
	 */
	public InterfaceAlterarTipoVeiculo(){
		initComponents();
		
		this.setTitle("Alterações em Tipo Veículo");
		
		jButton1.setVisible(true);
	     jButton2.setVisible(true);
	     jButton3.setVisible(false);
	     jButton4.setVisible(false);
	     jButton5.setVisible(true);
	     jButton6.setVisible(true);
	     jLabel1.setVisible(true);
	     jLabel2.setVisible(false);
	     jLabel3.setVisible(false);
		jPanel1.setVisible(true);
		jTextField1.setEditable(true);
		jTextField1.setText("");
		jTextField1.setVisible(true);
		jTextField2.setVisible(false);
		jTextField3.setVisible(false);
	}
	//fim construtor
	
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Alterando Dados", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Ubuntu Light", 0, 18), new java.awt.Color(76, 76, 76))); // NOI18N

        jButton3.setText("Confirmar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonConfirmaActionPerformed(evt);
            }
        });

        jButton4.setText("Cancelar");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelarActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Ubuntu Light", 0, 18)); // NOI18N
        jLabel1.setText("Id : ");

        jLabel2.setFont(new java.awt.Font("Ubuntu Light", 0, 18));
        jLabel2.setText("Nome da Categoria : ");

        jLabel3.setFont(new java.awt.Font("Ubuntu Light", 0, 18));
        jLabel3.setText("Percentual : ");

        jButton5.setText("Ok");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOkActionPerformed(evt);
            }
        });

        jButton6.setText("Listar Tipos de Veículos");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
				try {
					jButtonListarTipoVeiculoActionPerformed(evt);
				} catch (IOException ex) {
					Logger.getLogger(InterfaceAlterarTipoVeiculo.class.getName()).log(Level.SEVERE, null, ex);
				}
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(261, Short.MAX_VALUE)
                .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(6, 6, 6)
                .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(25, 25, 25))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 288, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(324, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(93, 93, 93)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jTextField2)
                    .addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 257, Short.MAX_VALUE))
                .addContainerGap(308, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                 .addGap(37, 37, 37)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton5))
                .addGap(70, 70, 70)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3)))
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 120, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3)
                    .addComponent(jButton4)
                    .addComponent(jButton6))
                .addGap(59, 59, 59))
        );

        jButton1.setText("Logoff");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLogoffActionPerformed(evt);
            }
        });

        jButton2.setText("Voltar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonVoltarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 537, Short.MAX_VALUE)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(47, 47, 47)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 488, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(30, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

	/* 
	 * Método botão log off que retornar para a janela de login
	 * @param evt
	 */
	private void jButtonLogoffActionPerformed(java.awt.event.ActionEvent evt) {                                         
		dispose();
		ControleAcesso.logoff();
	}                                        

	/*
	 * Método botão Voltar  
	 * @param evt
	 */
	private void jButtonVoltarActionPerformed(java.awt.event.ActionEvent evt) {                                         
		dispose();
		ControleAcesso.voltarMenuPrincipal();
	}  
	//fim método

	/*
	 * Método do botão cancelar que seta a janela com os campos em branco
	 */
	private void jButtonCancelarActionPerformed(java.awt.event.ActionEvent evt) {                                         
		jButton1.setVisible(true);
	     jButton2.setVisible(true);
	     jButton3.setVisible(false);
	     jButton4.setVisible(false);
	     jButton5.setVisible(true);
	     jButton6.setVisible(true);
	     jLabel1.setVisible(true);
	     jLabel2.setVisible(false);
	     jLabel3.setVisible(false);
		jPanel1.setVisible(true);
		jTextField1.setEnabled(true);
		jTextField1.setText("");
		jTextField1.setVisible(true);
		jTextField1.setText("");
		jTextField2.setVisible(false);
		jTextField1.setText("");
		jTextField3.setVisible(false);
	}                                        
	//fim método
	
	/*
	 * Método do botão confirmar que após a o botão ok listar a alteração, 
	 * a tela mostra campos para realizar alteração em um tipo de veículo
	 * @param evt
	 */
	private void jButtonConfirmaActionPerformed(java.awt.event.ActionEvent evt) {  
		int idPesquisa = Integer.parseInt(jTextField1.getText());
		String categoria = jTextField2.getText();
		String adicional = jTextField3.getText();
		TipoVeiculo tipoVeiculo = new TipoVeiculo(idPesquisa);
		TipoVeiculo tipoVeiculo1 = (TipoVeiculo) persistenciaTipoVeiculo.consultar(tipoVeiculo);
		
		if(categoria.equals("") && adicional.equals("") ){
			JOptionPane.showMessageDialog(null,"<html> <font color='black'>Por favor, preencha pelo menos um campo!","Erro",JOptionPane.ERROR_MESSAGE);
		}else{
			if(!categoria.equals("")){
				tipoVeiculo1.setCategoria(categoria);	
			}
			if(!adicional.equals("")){
				tipoVeiculo1.setPercentual(Double.parseDouble(adicional));	
			}
			jButton1.setVisible(true);
			jButton2.setVisible(true);
			jButton3.setVisible(false);
			jButton4.setVisible(false);
			jButton5.setVisible(true);
			jButton6.setVisible(true);
			jLabel1.setVisible(true);
			jLabel2.setVisible(false);
			jLabel3.setVisible(false);
			jPanel1.setVisible(true);
			jTextField1.setEnabled(false);
			jTextField1.setText("");
			jTextField1.setVisible(true);
			jTextField1.setText("");
			jTextField2.setVisible(false);
			jTextField1.setText("");
			jTextField3.setVisible(false);
			persistenciaTipoVeiculo.atualizar(tipoVeiculo1);
			JOptionPane.showMessageDialog(null,"<html> <font color='black'>Alteração concluída!","Parabéns!",JOptionPane.INFORMATION_MESSAGE);
		}
	}                                        
	// fim método
	
	/*
	 * Método do botão ok que faz uma pesquisa pelo id digitado pelo usuario 
	 * em tipo veiculo 
	 * @param evt
	 */
	private void jButtonOkActionPerformed(java.awt.event.ActionEvent evt) {  
		String id = jTextField1.getText();                                       
		
		if(!id.equals("")){
			int idPesquisa = Integer.parseInt(id);
			TipoVeiculo tipoVeiculo = new TipoVeiculo(idPesquisa);
			TipoVeiculo tipoVeiculo1 = (TipoVeiculo) persistenciaTipoVeiculo.consultar(tipoVeiculo);
		
			if(tipoVeiculo1 != null){
				jButton1.setVisible(true);
				jButton2.setVisible(true);
				jButton3.setVisible(true);
				jButton4.setVisible(true);
				jButton5.setVisible(true);
				jButton6.setVisible(true);
				jLabel1.setVisible(true);
				jLabel2.setVisible(true);
				jLabel3.setVisible(true);
				jPanel1.setVisible(true);
				jTextField1.setEnabled(false);
				jTextField1.setVisible(true);
				jTextField2.setVisible(true);
				jTextField3.setVisible(true);
			}else {
				JOptionPane.showMessageDialog(null,"<html> <font color='black'>Não exite Tipo de Veículo com esse Id!","Erro",JOptionPane.ERROR_MESSAGE);
				jButton1.setVisible(true);
				jButton2.setVisible(true);
				jButton3.setVisible(false);
				jButton4.setVisible(false);
				jButton5.setVisible(true);
				jButton6.setVisible(true);
				jLabel1.setVisible(true);
				jLabel2.setVisible(false);
				jLabel3.setVisible(false);
				jPanel1.setVisible(true);
				jTextField1.setEnabled(true);
				jTextField1.setText("");
				jTextField1.setVisible(true);
				jTextField2.setText("");
				jTextField2.setVisible(false);
				jTextField3.setText("");
				jTextField3.setVisible(false);
			}
		}else{
			JOptionPane.showMessageDialog(null,"<html> <font color='black'>Digite um Id por favor!","Erro",JOptionPane.ERROR_MESSAGE);
		}
	}                                        
	//fim método
	
	/*
	 * Mètodo do botão que lista os tipo de veículo, chamando a interface 
	 * listagem Veiculos
	 * @param evt
	 * @param throws
	 */
	private void jButtonListarTipoVeiculoActionPerformed(java.awt.event.ActionEvent evt) throws IOException {
		new InterfaceListagemTipoVeiculo().setVisible(true);
	}
	//fim método
	
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
}
// fim classe